import { _decorator, Component, Node, Button } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { udm } from '../../../../scripts/managers/UserDataManager';
import { EditBox } from 'cc';
import { BindEditBoxInputComplete } from '../../../../scripts/libs/rxjs/cc3/BindEditBox';
import { pfm } from '../../../../scripts/managers/PlayFabManager';
const { ccclass, property } = _decorator;

@ccclass('PlayfabDebugPanel')
export class PlayfabDebugPanel extends BasePanel {

    @property(Button)
    closeButton: Button = null;

    @property(Button)
    loginButton: Button = null;

    @property(Button)
    updateUserDataButton: Button = null;

    @property(Button)
    getUserDataButton: Button = null;

    @property(Button)
    getProfileButton: Button = null;

    @property(Button)
    getLeaderboardButton: Button = null;

    titleId = 'A8D48';

    @property(EditBox)
    displayNameEditBox: EditBox = null;

    willBind(): void {
        PlayFab.settings.titleId = this.titleId;
    }

    useObserves() {
        return [
            BindButton(this.closeButton).subscribe(this.closeButtonClicked.bind(this)),
            BindButton(this.loginButton).subscribe(this.loginButtonClicked.bind(this)),
            BindButton(this.updateUserDataButton).subscribe(this.updateUserDataButtonClicked.bind(this)),
            BindButton(this.getUserDataButton).subscribe(this.getUserDataButtonClicked.bind(this)),
            BindButton(this.getProfileButton).subscribe(this.getProfileButtonClicked.bind(this)),
            BindButton(this.getLeaderboardButton).subscribe(this.getLeaderboardButtonClicked.bind(this)),
            udm.displayName.subscribe(_ => {
                if (_ != null) {
                    this.displayNameEditBox.string = _;
                }
            }),
            BindEditBoxInputComplete(this.displayNameEditBox).subscribe(_ => {
                pfm.setDisplayName(this.displayNameEditBox.string).subscribe(_ => this.log('@setDisplayName', _));
            }),
        ];
    }

    closeButtonClicked() {
        PlayfabDebugPanel.CloseProcess(null, 'close');
    }

    loginButtonClicked() {
        this.log('@loginButtonClicked');
        pfm.loginWithCustomId(udm.uid.value).subscribe(_ => this.log('@loginWithCustomId', _));
    }

    updateUserDataButtonClicked() {
        pfm.updateUserData(udm.stringify()).subscribe();
    }

    getProfileButtonClicked() {
        pfm.getProfile().subscribe(_ => { this.log('@getProfile', _) });
    }

    getUserDataButtonClicked() {
        pfm.getUserData().subscribe();
    }

    getLeaderboardButtonClicked() {
        pfm.getLeaderboard('stageNum_manual', 1, 20).subscribe();
    }
}

